<?php
//订单推送机器人
class BuyRobotService {
	//单位是分钟
	private $_minutes = 60;
	
	public function getNewOrderById($id, $status) {
		
		//机器人瓦力
		if ($status == 0) {
			return $this->getNewOrder ( $id, $status );
		}
		
		if ($status == 1) {
			return $this->batchOrder ( 0 );
		}
	
	}
	
	private function getNewOrder($id, $status) {
		$id = empty ( $id ) ? 0 : $id;
		$os = new BuyOrderService ();
		$order = $os->getOrderGreaterThanId ( $id, $status );
		
		if (empty ( $order )) {
			$rs = array ('status' => false, 'message' => "什么也没找到" );
		} else {
			$rs = array ('status' => true, 'message' => "发现新订单" );
		}
		return $rs;
	}
	
	//处理超过1小时的订单
	private function batchOrder($status) {
		$time = time ();
		$os = new BuyOrderService ();
		$order = $os->getOrderByStatus ( $status );
		
		$minTime = $this->_minutes * 60;
		if (empty ( $order )) {
			return $this->level ( 0 );
		}
		$i = 0;
		foreach ( $order as $v ) {
			if (round ( $v ['money'] ) < 3000) {
				continue;
			}
			
			if ($time - $v ['add_time'] < $minTime) {
				continue;
			}
			
			//处理订单
			$os->setStatusBySNO ( 1, $v ['orderno'], array('real_name'=>'瓦力') );
			$i ++;
		}
		
		return $this->level ( $i );
	}
	
	private function level($num) {
		switch ($num) {
			case $num < 1 :
				$rs = "什么也没找到";
				break;
			case $num < 2 :
				$rs = "小手一抖";
				break;
			case $num < 100 :
				$rs = "开了挖掘机";
				break;
			default :
				$rs = "什么也没找到";
				break;
		
		}
		$status = $num > 0 ? true : false;
		$message = $rs . "发现了 " . $num . "个订单";
		return array ('status' => $status, 'message' => $message );
	}

}